package com.linq.cool.system.service.impl;

import com.linq.cool.api.vo.LoginUser;
import com.linq.cool.common.core.utils.StringUtils;
import com.linq.cool.system.service.SysOnlineUserService;
import com.linq.cool.system.vo.SysOnlineUser;
import org.springframework.stereotype.Service;

/**
 * @Author: yqlin
 * @Date: 2021/1/29 08:31
 * @Description:
 * @Version: 1.0.0
 */
@Service
public class SysOnlineUserServiceImpl implements SysOnlineUserService {
    @Override
    public SysOnlineUser getByIp(String ip, LoginUser vo) {
        if (StringUtils.equals(ip, vo.getLoginIp())) {
            return this.loginUserToOnlineUser(vo);
        }
        return null;
    }

    @Override
    public SysOnlineUser getByUsername(String username, LoginUser vo) {
        if (StringUtils.equals(username, vo.getUsername())) {
            return this.loginUserToOnlineUser(vo);
        }
        return null;
    }

    @Override
    public SysOnlineUser getByParams(String ip, String username, LoginUser vo) {
        if (StringUtils.equals(username, vo.getUsername()) && StringUtils.equals(ip, vo.getLoginIp())) {
            return this.loginUserToOnlineUser(vo);
        }
        return null;
    }

    @Override
    public SysOnlineUser loginUserToOnlineUser(LoginUser user) {
        if (StringUtils.isNull(user)) {
            return null;
        }
        SysOnlineUser vo = new SysOnlineUser();
        vo.setTokenId(user.getToken())
                .setUsername(user.getUsername())
                .setIp(user.getLoginIp())
                .setLoginTime(user.getLoginTime());
        return vo;
    }
}
